package com.charging.business.media;

import com.charging.business.utils.ToolsUtil;

/**
 * OBU DFEF04内卡0015文件(发行基本数据文件))编码信息
 * 20个字节
 */
public class Card0015Ed20Model {
    public static final int MSG_LEN = 20;

    /**
     * 发卡标识(01-08,8,cn)
     * 格式：AAAAAAAA + BBBB + CC + DD
     * AAAAAAAA ：省级行政区划代码，2 个汉字（4 个字节）；
     * BBBB ：运营商标识，压缩BCD码，河南为0x4101；
     * CC ：保留；DD ：密钥分散标识，取值为 01。
     * 样例：0xBAD3C4CF41010001
     */
    public byte[] issueSign = new byte[8];

    /**
     * 卡片类型(09-09,1,n)22-储值卡；23-记账卡
     */
    public byte[] cardType = new byte[1];

    /**
     * 卡片版本(10-10,1,cn)统一为0x40
     */
    public byte[] cardVersion = new byte[1];

    /**
     * 卡片网络(11-12,2,cn)0x4101
     */
    public byte[] cardWork = new byte[2];

    /**
     * 卡片内部编号(13-20,8,cn)
     * 由1 字节“初始化年份”、1 字节“初始化星期”、1 字节“卡片类型”和5字节“卡片序列号”组成
     */
    public byte[] cardNo = new byte[8];

    public static Card0015Ed20Model parse(byte[] buff) {
        int index = 0;
        Card0015Ed20Model ret = new Card0015Ed20Model();
        index += ToolsUtil.arrcopy(buff, index, ret.issueSign);
        index += ToolsUtil.arrcopy(buff, index, ret.cardType);
        index += ToolsUtil.arrcopy(buff, index, ret.cardVersion);
        index += ToolsUtil.arrcopy(buff, index, ret.cardWork);
        index += ToolsUtil.arrcopy(buff, index, ret.cardNo);

        return ret;
    }

    public static Card0015Ed20Model parse(String strHex) {
        if (strHex.isEmpty()) {
            return null;
        }
//		if (strHex.length()<UCARD0015ED20.MSG_LEN*2) {
//            return null;
//        }
        if (!ToolsUtil.chkHexChar(strHex)) {
            return null;
        }

        byte[] buff = ToolsUtil.hex2bin(strHex);

        return parse(buff);
    }

    /**
     * 信息解码
     *
     * @return
     */
    public Card001520Model decoded() {
        Card001520Model dd = new Card001520Model();
        dd.issueSign = ToolsUtil.bin2hex(issueSign);
        dd.cardType = ToolsUtil.bin2int(cardType);
        dd.cardVersion = ToolsUtil.bin2hex(cardVersion);
        dd.cardWork = ToolsUtil.bin2hex(cardWork);
        dd.cardNo = ToolsUtil.bin2hex(cardNo);

        String issArea = dd.issueSign.substring(0, 8);
        String netWork = dd.cardWork.substring(0, 2);
        String subNet = dd.cardWork.substring(2);

        issArea = ToolsUtil.hex2str(issArea, "gb2312");
        if ("河南".equals(issArea)) {
            netWork = "41" + subNet;
        } else if ("上海".equals(issArea)) {
            netWork = "31" + subNet;
        } else if ("江苏".equals(issArea)) {
            netWork = "32" + subNet;
        } else if ("浙江".equals(issArea)) {
            netWork = "33" + subNet;
        } else if ("安徽".equals(issArea)) {
            netWork = "34" + subNet;
        } else if ("福建".equals(issArea)) {
            netWork = "35" + subNet;
        } else if ("江西".equals(issArea)) {
            netWork = "36" + subNet;
        } else if ("山东".equals(issArea)) {
            netWork = "37" + subNet;
        } else {
            netWork += subNet;
        }
        dd.cardNetId = netWork;


        return dd;
    }
}
